<?php
/**
 * 用户模块接口
 * User: cqfut
 * Date: 2018/11/1
 * Time: 11:44
 */
//require_once (ROOTDIR."admin/model/csrf_check.php");
require_once ("../../config/path_config.php");
include_once (ROOTDIR."common/base_router.php");
require_once (ROOTDIR."config/sys_config.php");
require_once (ROOTDIR."admin/model/user_model.php");

class UserRouter extends BaseRouter {
    public function __construct() {
        parent::__construct();
    }

    function addUser() {
        $args = array();
        array_push($args, 'id', 'name', 'school', 'email', 'vcode');
        // 必要参数判断
        if ( ! $this->checkParam($args) ) {
            $this->setRspStatus(0, -2,"缺少必要参数" );
            return;
        }
        $vcode = $this->getReqStrVal('vcode'); // 验证码
        if (strtolower($vcode) == strtolower($_SESSION['vcode']) ) {
            $this->setRspStatus(0, -3,"验证码错误" );
            return;
        }


        $id = $this->getReqStrVal('id'); // 用户名
        $name = $this->getReqStrVal('name'); // 昵称
        $email = $this->getReqStrVal('email'); // 邮箱
        $school = $this->getReqStrVal('school');
        // $type $createrId 取值为如下两个值时，表示是外部注册用户
        // 谨以2020年3月22日纪念自己暂离教育行业
        $type = 20200322;
        $createrId='register';
        $model = new UserModel();

        // 判断用户是否已存在
        if ( $model->isExist($id) > 0 ) {
            $this->setRspStatus(0, -1,"用户已存在" );
            return;
        }

        // 邮箱已被注册
        if ( $model->isExistMail($email) > 0 ) {
            $this->setRspStatus(0, -2,"邮箱已被注册" );
            return;
        }

        $createrId = BaseRouter::getSession('user_id');
        $ret = $model->addUser($id, $name, $school, $status,
            $type, $inzuozhu, $grade, $upordown, $phone,
            $gradestr, $class, $createrId);
        if ( $ret >= 0 ) {
            $this->setRspStatus(1, 0,"添加用户信息成功" );
        }
        else {
            $this->setRspStatus(0, -1,"数据库错误" );
        }
    }

    function editUser() {
        $args = array();
        array_push($args, 'id', 'name', 'school', 'type');
        // 必要参数判断
        if ( ! $this->checkParam($args) ) {
            $this->setRspStatus(0, -2,"缺少必要字段" );
            return;
        }

        $createrId = null;
        // 判断是否具有所有用户的编辑权限
        if (  BaseRouter::getSession('jgadmin') == null
            && BaseRouter::getSession('administrator') == null ) {
            $createrId = BaseRouter::getSession('user_id');
        }

        $id = $this->getReqStrVal('id');
        $name = $this->getReqStrVal('name');
        $school = $this->getReqStrVal('school');
        $status = $this->getReqIntVal('status');
        $type = $this->getReqIntVal('type');
        $inzuozhu = $this->getReqIntVal('inzuozhu');
        $grade = $this->getReqIntVal('grade'); // 表示年级的数字，旧版
        $upordown = $this->getReqIntVal('upordown');
        $phone = $this->getReqStrVal('phone');
        $gradestr = $this->getReqStrVal('gradestr'); // 初2019级
        $class = $this->getReqIntVal('class'); // 2 - 2班

        $model = new UserModel();

        // 系统管理员administrator不允许被编辑
        if ( $model->isAdmin($id) > 0 ) {
            $this->setRspStatus(0, -1,"权限错误" );
            return;
        }

        // 非 jgadmin 和 administrator 只能编辑自己创建的用户
        $ret = $model->editUser($id, $name, $school, $status,
            $type, $inzuozhu, $grade, $upordown, $phone,
            $gradestr, $class, $createrId);
        if ( $ret > 0 ) {
            $this->setRspStatus(1, 0,"修改用户信息成功" );
        }
        else {
            $this->setRspStatus(0, -1,"数据库错误" );
        }
    }

    public function run() {
        $op = $this->getReqVal('op');
        if ( $op == 'add' ) {
            $this->addUser();
        }
        else if ( $op == 'del' ) {
            $this->delUser();
        }
        else if ( $op == 'edit' ) {
            $this->editUser();
        }
        else if ( $op == 'search' ) {
            $this->getUserList();
        }
        else if ( $op == 'check' ) {
            $this->checkPrivileges();
        }
        else if ( $op == 'get_grade_list') {
            $this->getGradeList();
        }
        else if ( $op == 'import') {
            $this->importUsers();
        }
        else if ( $op == 'export') {
            $this->exportUsers();
        }
        else {
            $this->setRspStatus(0, -1,"参数错误" );
        }
    }
}

$router = new UserRouter();
$router->init();
$router->run();
$router->finish();